/**
 * Copyright 2010 Jake Jackson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jake.nb.ext.ant.hints.impl;

import java.util.Collection;
import java.util.HashSet;
import javax.swing.text.Document;
import org.jake.nb.ext.ant.hints.AntHint;
import org.jake.nb.ext.ant.hints.Configuration;
import org.jake.nb.ext.ant.hints.Paths;
import org.jake.nb.ext.ant.hints.SpanContainer;
import org.jake.nb.ext.ant.parser.AntcallSpan;
import org.jake.nb.ext.ant.parser.Span;
import org.netbeans.modules.editor.NbEditorUtilities;
import org.netbeans.spi.editor.hints.ErrorDescription;
import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
import org.netbeans.spi.editor.hints.Severity;
import org.openide.util.NbBundle;
import org.openide.util.lookup.ServiceProvider;

/**
 * An ant hint checking antcall's target is not empty resp. existing.
 * @version 0.2
 * @author JakeJ
 * @since
 */
@ServiceProvider(service=AntHint.class, path="org-jake-nb-ext-ant-hints")
public class EmtptyTargetInAntCall implements AntHint {

  private Configuration configuration;

  public EmtptyTargetInAntCall() {
    configuration = new Configuration(EmtptyTargetInAntCall.class.getCanonicalName(), "Empty target in antcall",
            "Indicates the target attribute of a antcall task is empty.", true, Severity.ERROR, Paths.ANTCALL);
  }

  @Override
  public Collection<ErrorDescription> getErrors(final SpanContainer spanContainer) {
    final Collection<ErrorDescription> errors = new HashSet<ErrorDescription>();
    for (final Span span : spanContainer.getAntallSpans().getSpans()) {
      final AntcallSpan acSpan = (AntcallSpan)span;
      if(acSpan.getTargetSpan() != null) {
        final String name = acSpan.getTargetName();
        if(name.trim().length() == 0) {
          errors.add(createError(spanContainer.getDocument(), acSpan));
        }
      }
    }
    return errors;
  }

  private ErrorDescription createError(final Document document, final AntcallSpan span)  {

    final String text = NbBundle.getMessage(EmtptyTargetInAntCall.class, "Desc_Err_AntCallTargetEmpty"); // NOI18N

    return ErrorDescriptionFactory.createErrorDescription(configuration.getSeverity(), text,
                document, NbEditorUtilities.getLine(document, span.getStart(), true).getLineNumber() + 1);
  }

  @Override
  public Configuration getConfiguration() {
    return configuration;
  }
}
